package jgl.vajra.driver.widget.utils;

import android.text.TextUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Created by Administrator on 2019/11/29.
 */

public class TimeUtils {
    public static final SimpleDateFormat DEFAULT_SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
    public static final SimpleDateFormat DEFAULT_SDF2 = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
    public static final SimpleDateFormat DEFAULT_SDF3 = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
    public static final SimpleDateFormat DEFAULT_SDF4 = new SimpleDateFormat("yyyy年MM月dd日", Locale.getDefault());

    public static final SimpleDateFormat DEFAULT_SDF5 = new SimpleDateFormat("yyyy", Locale.getDefault());
    public static final SimpleDateFormat DEFAULT_SDF6 = new SimpleDateFormat("MM", Locale.getDefault());
    public static final SimpleDateFormat DEFAULT_SDF7 = new SimpleDateFormat("dd", Locale.getDefault());
    public static final SimpleDateFormat DEFAULT_SDF8 = new SimpleDateFormat("yyyy.MM.dd", Locale.getDefault());
    public static final SimpleDateFormat DEFAULT_SDF9 = new SimpleDateFormat("MM月dd日 HH:mm", Locale.getDefault());
    public static final SimpleDateFormat DEFAULT_SDF10 = new SimpleDateFormat("HH:mm", Locale.getDefault());
    public static final SimpleDateFormat DEFAULT_SDF11 = new SimpleDateFormat("HH:mm:ss", Locale.getDefault());
    public static final SimpleDateFormat DEFAULT_HOUR = new SimpleDateFormat("HH", Locale.getDefault());
    public static final SimpleDateFormat DEFAULT_MOUTH_DAY = new SimpleDateFormat("MM-dd HH:mm", Locale.getDefault());

    public final static long minute = 60;// 1分钟
    public final static long hour = 60 * minute;// 1小时
    public final static long day = 24 * hour;// 1天
    public final static long month = 31 * day;// 月
    public final static long year = 12 * month;// 年


    public static String getParseDayTextTime(Date date) {//可根据需要自行截取数据显示
        SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
        return format.format(date);
    }

    public static String parseYearTime(long utcTime) {
        return parseTimeLong(utcTime, DEFAULT_SDF5);
    }

    public static String parseHourTime(long utcTime) {
        return parseTimeLong(utcTime, DEFAULT_HOUR);
    }

    public static String parseMouthDayTime(long utcTime) {
        return parseTimeLong(utcTime / 1000, DEFAULT_MOUTH_DAY);
    }

    public static String parseHourMTime(long utcTime) {
        return parseTimeLong(utcTime, DEFAULT_SDF10);
    }

    public static String parseHourMSTime(long utcTime) {
        return parseTimeLong(utcTime, DEFAULT_SDF11);
    }

    public static String parseMonthTime(long utcTime) {
        return parseTimeLong(utcTime, DEFAULT_SDF6);
    }

    public static String parseDayTime(long utcTime) {
        return parseTimeLong(utcTime, DEFAULT_SDF7);
    }

    public static String parseTimeLong(long utcTime) {
        return parseTimeLong(utcTime, DEFAULT_SDF);
    }

    public static String parseHMTimeLong(long utcTime) {

        return parseTimeLong(utcTime, DEFAULT_SDF3);
    }

    public static String parseDayTimeLong(long utcTime) {

        return parseTimeLong(utcTime, DEFAULT_SDF2);
    }

    public static long parsDataTimeLong(String utcTime) {
        if (TextUtils.isEmpty(utcTime)) return 0;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = simpleDateFormat.parse(utcTime);
            long ts = date.getTime();
            return ts;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static String parseDayTextTimeLong(long utcTime) {

        return parseTimeLong(utcTime, DEFAULT_SDF8);
    }

    public static String parseTimeLong(long milliseconds, SimpleDateFormat format) {
        try {
            return format.format(new Date(milliseconds * 1000));

        } catch (Exception e) {
            return "";
        }
    }

    public static String parseTimeLong(String milliseconds, SimpleDateFormat format) {
        try {
            return format.format(new Date(Long.parseLong(milliseconds) * 1000));

        } catch (Exception e) {
            return "";
        }
    }


    public static String getHourMinSecondTime(int second) {
        int h = 0;
        int d = 0;
        int s = 0;
        int temp = second % 3600;
        if (second >= 3600) {
            h = second / 3600;
            if (temp != 0) {
                if (temp > 60) {
                    d = temp / 60;
                    if (temp % 60 != 0) {
                        s = temp % 60;
                    }
                } else {
                    s = temp;
                }
            }
        } else {
            d = second / 60;
            if (second % 60 != 0) {
                s = second % 60;
            }
        }

        if (second == 0)
            return "";

        if (second < 3600 && second >= 60)
            return d + "分" + s + "秒";

        if (second < 60 && second > 0)
            return s + "秒";

        return h + "时" + d + "分" + s + "秒";

    }

    /**
     * 返回文字描述的日期
     *
     * @param date
     * @return
     */
    public static String getTimeFormatText(long date) {

        long diff = (new Date().getTime() - date) / 1000;
        long r = 0;
        if (diff > year) {
            r = (diff / year);
            return r + "年前";
        }
        if (diff > month) {
            r = (diff / month);
            return r + "个月前";
        }
        if (diff > day) {
            r = (diff / day);
            return r + "天前";
        }
        if (diff > hour) {
            r = (diff / hour);
            return r + "个小时前";
        }
        if (diff > minute) {
            r = (diff / minute);
            return r + "分钟前";
        }
        return "刚刚";
    }

    public static long dateDiffHour(long diffTime) {


        return diffTime % day / hour;// 计算差多少小时
    }

    public static long dateDiffDay(long diffTime) {

        long nd = 24 * 60 * 60;// 一天的毫秒数
        return diffTime / nd;// 计算差多少小时
    }


    public static long dateDiffMinute(long diffTime) {
        return diffTime % day % hour / minute;// 计算差多少分钟
    }

    public static long dateDiffSecond(long diffTime) {

        return diffTime % day % hour % minute;
    }

    public static boolean isCloseEnough(long createAt, long preCreateAt) {
        return preCreateAt - createAt < 60;
    }

    public static String formatMusicTime(long duration) {
        String time = "";
        long minute = duration / 60000;
        long seconds = duration % 60000;
        long second = Math.round((int) seconds / 1000);
        if (minute < 10) {
            time += "0";
        }
        time += minute + ":";
        if (second < 10) {
            time += "0";
        }
        time += second;
        return time;
    }

    public static String dateDiffHMSTime(long duration) {
        long hour = TimeUtils.dateDiffHour(duration);
        long minute = TimeUtils.dateDiffMinute(duration);
        long second = TimeUtils.dateDiffSecond(duration);
        StringBuilder sb = new StringBuilder();
        if (hour > 0) {
            sb.append(hour).append("小时");
        }
        if (minute > 0) {
            sb.append(minute).append("分钟");

        }
        if (second > 0) {
            sb.append(second).append("秒");

        }
        return sb.toString();
    }

    public static String getTodayOrTime(long date) {//date 是存储的时间戳
       /* //所在时区时8，系统初始时间是1970-01-01 80:00:00，注意是从八点开始，计算的时候要加回去
        int offSet = Calendar.getInstance().getTimeZone().getRawOffset();
        long today = (System.currentTimeMillis() + offSet) / 86400000;
        long start = (date + offSet) / 86400000;
        long intervalTime = start - today;
        //-2:前天,-1：昨天,0：今天,1：明天,2：后天
        String strDes = "";
        if (intervalTime == 0) {
            strDes = "今天" + parseTimeLong(date/1000, DEFAULT_SDF10);
        } else if (intervalTime == 1) {
            strDes = "明天" + parseTimeLong(date/1000, DEFAULT_SDF10);
        } else {
            strDes = parseTimeLong(date/1000, DEFAULT_SDF9);
            ;//直接显示时间
        }*/
        return parseTimeLong(date / 1000, DEFAULT_SDF9);
    }

    public static String getDayTime(String time) {
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = sdf1.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
        String str = sdf2.format(date);
        return str;
    }

    //是否在两天以内
    public static boolean isWithinTwoDays(String time) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d = null;
        try {
            d = df.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        cal.add(Calendar.DATE, -2); //减2天
        String format = df.format(cal.getTime());

        Date nowTime = null;
        Date beginTime = null;
        Date endTime = null;
        try {
            nowTime = df.parse(df.format(new Date()));
            beginTime = df.parse(format);
            endTime = df.parse(time);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(beginTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }

    public static   int getHMSTimeSecond(String timeTemp) {
        int time = 0;
        Calendar instance = Calendar.getInstance();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
        try {
            Date parse = simpleDateFormat.parse(timeTemp);
            instance.setTime(parse);
            int hour = instance.get(Calendar.HOUR);
            int minute = instance.get(Calendar.MINUTE);
            int second = instance.get(Calendar.SECOND);
            time = hour * 60 * 60 + minute * 60 + second;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return time;
    }

}
